package com.ruoyi.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.ruoyi.system.domain.SysCompeScoringAssgin;
import com.ruoyi.system.domain.SysUserJudges;
import com.ruoyi.system.domain.SysUsers;
import com.ruoyi.system.domain.SyseEventsPosted;
import com.ruoyi.system.domain.mongodb.Users;
import com.ruoyi.system.mapper.SysUsersMapper;
import com.ruoyi.system.service.ISysCompeScoringAssginService;
import com.ruoyi.system.service.ISysUsersService;
import com.ruoyi.system.service.WorkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@Api("评委信息管理")
@RestController
@RequestMapping("/system/users")
public class SysUsersController {
    private static Log log = LogFactory.getLog(SysUsersController.class);

    @Autowired
    private ISysUsersService usersService;

    @Autowired
    private ISysCompeScoringAssginService iSysCompeScoringAssginService;

    @Autowired
    private WorkService workService;

    @ApiOperation("获取评委列表")
    @GetMapping("/readUsersAll")
    private PageInfo<SysUserJudges> readUsersAll(Integer epid,String nickName, String epschool,Integer pageNum,Integer pageSize){
        int currentPage = pageNum/pageSize+1;
        List<SysUserJudges> sysUserJudges = usersService.readUserAll(epid,nickName,epschool,currentPage,pageSize);
        PageInfo pageInfo = new PageInfo(sysUserJudges);
        return pageInfo;
    }

    @ApiOperation("更新评委账号状态")
    @ApiImplicitParam(name = "userId", value = "用户ID" , required = true)
    @PostMapping("/updateStatus")
    private long updateStatus(Integer userId){
        //根据评委id删除评委作品信息
        iSysCompeScoringAssginService.deleteSysCompeScoringAssginByUserId(userId);
        return  usersService.updateStatus(userId);
    }

    @ApiOperation("获取所有赛事")
    @GetMapping("/readEventAll")
    private Map<String, Object> readEventAll(SyseEventsPosted eventsPosted){
        return usersService.readEventAll(eventsPosted);
    };

    @ApiOperation("获取不是评委列表")
    @GetMapping("/readNoUser")
    private PageInfo<SysUsers> readNoUser(SysUsers sysUsers,Integer pageNum,Integer pageSize){
        System.out.println(sysUsers);
        int currentPage = pageNum/pageSize+1;
        List<SysUsers> sysUsersList = usersService.readNoUser(sysUsers,currentPage,pageSize);
        PageInfo pageInfo = new PageInfo(sysUsersList);
        return pageInfo;
    }

    @ApiOperation("为学校查询不是评委列表")
    @GetMapping("/readNoUserForSchool")
    private PageInfo<Users> readNoUserForSchool(SysUsers sysUsers, String epschool, Integer pageNum, Integer pageSize){
        System.out.println(sysUsers);
        int currentPage = pageNum/pageSize+1;
        List<Users> sysUsersList = usersService.readNoUserForSchool(sysUsers, epschool, currentPage,pageSize);
        PageInfo pageInfo = new PageInfo(sysUsersList);
        return pageInfo;
    }

    @ApiOperation("批量新增评委")
    @PostMapping("/moreAddJudges")
    private long moreAddJudges(String[] userIds,String epid,String scoring_step){
        for (String  userId:userIds){
            //读取不是评委
            SysUsers sysUsers1 = usersService.readInsertUser(Integer.parseInt(userId));
            sysUsers1.setIsJudges('1');
            if (null!=sysUsers1){
                //新增评委表
                long l = usersService.insertUser(sysUsers1);
                log.info(l);
                if (l>0){
                    //获取最后增加的Id
                    long lastUserId = usersService.readLastUserId();
                    //新增联合表 联合表为后面添加的那条数据
                    long insertUserEvents = usersService.insertUserEvent(lastUserId,epid);
                    if (insertUserEvents>0){
                            //删除原来不是评委表 再删除联合表
                            long l1 = usersService.deleteUser(sysUsers1.getUserId());
                    }
                    //根据用户id,查询负责赛事下的作品id
                    List<String> workIds = workService.queryWorkIdByUserId(lastUserId);
                    for (String workId : workIds) {
                        //添加用户id和作品id到作品用户的分派任务表中
                        SysCompeScoringAssgin sysCompeScoringAssgin = new SysCompeScoringAssgin();
                        sysCompeScoringAssgin.setUser_id((int)lastUserId);
                        sysCompeScoringAssgin.setWork_id(workId);
                        sysCompeScoringAssgin.setEvent_id(Integer.parseInt((epid)));
                        sysCompeScoringAssgin.setScoring_step(scoring_step);
                        iSysCompeScoringAssginService.saveSysCompeScoringAssgin(sysCompeScoringAssgin);
//                        SysUserWork sysUserWork=new SysUserWork();
//                        sysUserWork.setUser_id(lastUserId);
//                        sysUserWork.setWork_id(workId);
//                        sysUserWorkService.saveSysUserWork(sysUserWork);
                    }
                }
            }
        }
        return 1;
    }

    @ApiOperation("读取一条评委信息")
    @GetMapping("/readUserById")
    public SysUsers readUserById(Integer userId){
        return  usersService.readUserById(userId);
    };

    @ApiOperation("添加老师到评委")
    @RequestMapping("/addJudges")
    public long addJudges(SysUsers users){
        long l = usersService.addJudges(users);
        return l;
    }

    @ApiOperation("查询已发布的赛事")
    @GetMapping("/readEvents")
    public List<SyseEventsPosted> readEvents(String schoolName){
        return usersService.readEvents(schoolName);
    }

}
